let LoginHandler = function (){}

LoginHandler.prototype.listenSubmitEvent = function (){
  $("#submit-btn").on("click", function (event){
    event.preventDefault();
    let email = $("input[name='email']").val();
    let password = $("input[name='password']").val();
    let remember = $("input[name='remember']").prop("checked");
    csrf_ajax.post({
      url: "/login/",
      data: {
        email,
        password,
        remember: remember? 1: 0
      },
      success: function (result){
        if(result['code'] === 200){
          let token = result['data']['access_token'];
           let refreshToken = result['data']['refresh_token'];
          let user = result['data']['user'];
           // 存储双Token
          localStorage.setItem("ACCESS_TOKEN", token);
          localStorage.setItem("REFRESH_TOKEN", refreshToken);
          localStorage.setItem("USER_KEY", JSON.stringify(user));
          window.location = "/"
        }else if(result["code"] === 401 ){
          xtalert.alertErrorToast(result['message']);
        }
      }
    })
  });
}

LoginHandler.prototype.run = function (){
  this.listenSubmitEvent();
}


$(function (){
  let handler = new LoginHandler();
  handler.run();
});